/*
 * Created by the DBAccessor generator.
 * Programmer: Lennart Martens
 * Date: 24/01/2008
 * Time: 16:17:45
 */
package org.hkupp.db.accessors;

import be.proteomics.util.db.interfaces.Deleteable;
import be.proteomics.util.db.interfaces.Persistable;
import be.proteomics.util.db.interfaces.Retrievable;
import be.proteomics.util.db.interfaces.Updateable;

import java.sql.*;
import java.util.HashMap;

/*
 * CVS information:
 *
 * $Revision: 1.4 $
 * $Date: 2007/07/06 09:41:53 $
 */

/**
 * This class is a generated accessor for the Component table.
 *
 * @author DBAccessor generator class (Lennart Martens).
 */
public class ComponentTableAccessor implements Deleteable, Retrievable, Updateable, Persistable {

	/**
	 * This variable tracks changes to the object.
	 */
	protected boolean iUpdated = false;

	/**
	 * This variable can hold generated primary key columns.
	 */
	protected Object[] iKeys = null;

	/**
	 * This variable represents the contents for the 'componentid' column.
	 */
	protected long iComponentid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'l_instrumentid' column.
	 */
	protected long iL_instrumentid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'l_component_typeid' column.
	 */
	protected long iL_component_typeid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'order_index' column.
	 */
	protected long iOrder_index = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'creationdate' column.
	 */
	protected java.sql.Timestamp iCreationdate = null;


	/**
	 * This variable represents the contents for the 'modificationdate' column.
	 */
	protected java.sql.Timestamp iModificationdate = null;


	/**
	 * This variable represents the contents for the 'username' column.
	 */
	protected String iUsername = null;


	/**
	 * This variable represents the key for the 'componentid' column.
	 */
	public static final String COMPONENTID = "COMPONENTID";

	/**
	 * This variable represents the key for the 'l_instrumentid' column.
	 */
	public static final String L_INSTRUMENTID = "L_INSTRUMENTID";

	/**
	 * This variable represents the key for the 'l_component_typeid' column.
	 */
	public static final String L_COMPONENT_TYPEID = "L_COMPONENT_TYPEID";

	/**
	 * This variable represents the key for the 'order_index' column.
	 */
	public static final String ORDER_INDEX = "ORDER_INDEX";

	/**
	 * This variable represents the key for the 'creationdate' column.
	 */
	public static final String CREATIONDATE = "CREATIONDATE";

	/**
	 * This variable represents the key for the 'modificationdate' column.
	 */
	public static final String MODIFICATIONDATE = "MODIFICATIONDATE";

	/**
	 * This variable represents the key for the 'username' column.
	 */
	public static final String USERNAME = "USERNAME";




	/**
	 * Default constructor.
	 */
	public ComponentTableAccessor() {
	}

	/**
	 * This constructor allows the creation of the 'ComponentTableAccessor' object based on a set of values in the HashMap.
	 *
	 * @param	aParams	HashMap with the parameters to initialize this object with.
	 *		<i>Please use only constants defined on this class as keys in the HashMap!</i>
	 */
	public ComponentTableAccessor(HashMap aParams) {
		if(aParams.containsKey(COMPONENTID)) {
			this.iComponentid = ((Long)aParams.get(COMPONENTID)).longValue();
		}
		if(aParams.containsKey(L_INSTRUMENTID)) {
			this.iL_instrumentid = ((Long)aParams.get(L_INSTRUMENTID)).longValue();
		}
		if(aParams.containsKey(L_COMPONENT_TYPEID)) {
			this.iL_component_typeid = ((Long)aParams.get(L_COMPONENT_TYPEID)).longValue();
		}
		if(aParams.containsKey(ORDER_INDEX)) {
			this.iOrder_index = ((Long)aParams.get(ORDER_INDEX)).longValue();
		}
		if(aParams.containsKey(CREATIONDATE)) {
			this.iCreationdate = (java.sql.Timestamp)aParams.get(CREATIONDATE);
		}
		if(aParams.containsKey(MODIFICATIONDATE)) {
			this.iModificationdate = (java.sql.Timestamp)aParams.get(MODIFICATIONDATE);
		}
		if(aParams.containsKey(USERNAME)) {
			this.iUsername = (String)aParams.get(USERNAME);
		}
		this.iUpdated = true;
	}


	/**
	 * This constructor allows the creation of the 'ComponentTableAccessor' object based on a resultset
	 * obtained by a 'select * from Component' query.
	 *
	 * @param	aResultSet	ResultSet with the required columns to initialize this object with.
	 * @exception	SQLException	when the ResultSet could not be read.
	 */
	public ComponentTableAccessor(ResultSet aResultSet) throws SQLException {
		this.iComponentid = aResultSet.getLong("componentid");
		this.iL_instrumentid = aResultSet.getLong("l_instrumentid");
		this.iL_component_typeid = aResultSet.getLong("l_component_typeid");
		this.iOrder_index = aResultSet.getLong("order_index");
		this.iCreationdate = (java.sql.Timestamp)aResultSet.getObject("creationdate");
		this.iModificationdate = (java.sql.Timestamp)aResultSet.getObject("modificationdate");
		this.iUsername = (String)aResultSet.getObject("username");

		this.iUpdated = true;
	}


	/**
	 * This method returns the value for the 'Componentid' column
	 * 
	 * @return	long	with the value for the Componentid column.
	 */
	public long getComponentid() {
		return this.iComponentid;
	}

	/**
	 * This method returns the value for the 'L_instrumentid' column
	 * 
	 * @return	long	with the value for the L_instrumentid column.
	 */
	public long getL_instrumentid() {
		return this.iL_instrumentid;
	}

	/**
	 * This method returns the value for the 'L_component_typeid' column
	 * 
	 * @return	long	with the value for the L_component_typeid column.
	 */
	public long getL_component_typeid() {
		return this.iL_component_typeid;
	}

	/**
	 * This method returns the value for the 'Order_index' column
	 * 
	 * @return	long	with the value for the Order_index column.
	 */
	public long getOrder_index() {
		return this.iOrder_index;
	}

	/**
	 * This method returns the value for the 'Creationdate' column
	 * 
	 * @return	java.sql.Timestamp	with the value for the Creationdate column.
	 */
	public java.sql.Timestamp getCreationdate() {
		return this.iCreationdate;
	}

	/**
	 * This method returns the value for the 'Modificationdate' column
	 * 
	 * @return	java.sql.Timestamp	with the value for the Modificationdate column.
	 */
	public java.sql.Timestamp getModificationdate() {
		return this.iModificationdate;
	}

	/**
	 * This method returns the value for the 'Username' column
	 * 
	 * @return	String	with the value for the Username column.
	 */
	public String getUsername() {
		return this.iUsername;
	}

	/**
	 * This method sets the value for the 'Componentid' column
	 * 
	 * @param	aComponentid	long with the value for the Componentid column.
	 */
	public void setComponentid(long aComponentid) {
		this.iComponentid = aComponentid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'L_instrumentid' column
	 * 
	 * @param	aL_instrumentid	long with the value for the L_instrumentid column.
	 */
	public void setL_instrumentid(long aL_instrumentid) {
		this.iL_instrumentid = aL_instrumentid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'L_component_typeid' column
	 * 
	 * @param	aL_component_typeid	long with the value for the L_component_typeid column.
	 */
	public void setL_component_typeid(long aL_component_typeid) {
		this.iL_component_typeid = aL_component_typeid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Order_index' column
	 * 
	 * @param	aOrder_index	long with the value for the Order_index column.
	 */
	public void setOrder_index(long aOrder_index) {
		this.iOrder_index = aOrder_index;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Creationdate' column
	 * 
	 * @param	aCreationdate	java.sql.Timestamp with the value for the Creationdate column.
	 */
	public void setCreationdate(java.sql.Timestamp aCreationdate) {
		this.iCreationdate = aCreationdate;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Modificationdate' column
	 * 
	 * @param	aModificationdate	java.sql.Timestamp with the value for the Modificationdate column.
	 */
	public void setModificationdate(java.sql.Timestamp aModificationdate) {
		this.iModificationdate = aModificationdate;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Username' column
	 * 
	 * @param	aUsername	String with the value for the Username column.
	 */
	public void setUsername(String aUsername) {
		this.iUsername = aUsername;
		this.iUpdated = true;
	}



	/**
	 * This method allows the caller to delete the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int delete(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("DELETE FROM component WHERE componentid = ?");
		lStat.setLong(1, iComponentid);
		int result = lStat.executeUpdate();
		lStat.close();
		return result;
	}


	/**
	 * This method allows the caller to read data for this
	 * object from a persistent store based on the specified keys.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public void retrieve(Connection aConn, HashMap aKeys) throws SQLException {
		// First check to see whether all PK fields are present.
		if(!aKeys.containsKey(COMPONENTID)) {
			throw new IllegalArgumentException("Primary key field 'COMPONENTID' is missing in HashMap!");
		} else {
			iComponentid = ((Long)aKeys.get(COMPONENTID)).longValue()
;		}
		// In getting here, we probably have all we need to continue. So let's...
		PreparedStatement lStat = aConn.prepareStatement("SELECT * FROM component WHERE componentid = ?");
		lStat.setLong(1, iComponentid);
		ResultSet lRS = lStat.executeQuery();
		int hits = 0;
		while(lRS.next()) {
			hits++;
			iComponentid = lRS.getLong("componentid");
			iL_instrumentid = lRS.getLong("l_instrumentid");
			iL_component_typeid = lRS.getLong("l_component_typeid");
			iOrder_index = lRS.getLong("order_index");
			iCreationdate = (java.sql.Timestamp)lRS.getObject("creationdate");
			iModificationdate = (java.sql.Timestamp)lRS.getObject("modificationdate");
			iUsername = (String)lRS.getObject("username");
		}
		lRS.close();
		lStat.close();
		if(hits>1) {
			throw new SQLException("More than one hit found for the specified primary keys in the 'component' table! Object is initialized to last row returned.");
		} else if(hits == 0) {
			throw new SQLException("No hits found for the specified primary keys in the 'component' table! Object is not initialized correctly!");
		}
	}


	/**
	 * This method allows the caller to update the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int update(Connection aConn) throws SQLException {
		if(!this.iUpdated) {
			return 0;
		}
		PreparedStatement lStat = aConn.prepareStatement("UPDATE component SET componentid = ?, l_instrumentid = ?, l_component_typeid = ?, order_index = ?, creationdate = ?, modificationdate = CURRENT_TIMESTAMP, username = ? WHERE componentid = ?");
		lStat.setLong(1, iComponentid);
		lStat.setLong(2, iL_instrumentid);
		lStat.setLong(3, iL_component_typeid);
		lStat.setLong(4, iOrder_index);
		lStat.setObject(5, iCreationdate);
		lStat.setObject(6, iUsername);
		lStat.setLong(7, iComponentid);
		int result = lStat.executeUpdate();
		lStat.close();
		this.iUpdated = false;
		return result;
	}


	/**
	 * This method allows the caller to insert the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int persist(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("INSERT INTO component (componentid, l_instrumentid, l_component_typeid, order_index, creationdate, modificationdate, username) values(?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_USER)");
		if(iComponentid == Long.MIN_VALUE) {
			lStat.setNull(1, 4);
		} else {
			lStat.setLong(1, iComponentid);
		}
		if(iL_instrumentid == Long.MIN_VALUE) {
			lStat.setNull(2, 4);
		} else {
			lStat.setLong(2, iL_instrumentid);
		}
		if(iL_component_typeid == Long.MIN_VALUE) {
			lStat.setNull(3, 4);
		} else {
			lStat.setLong(3, iL_component_typeid);
		}
		if(iOrder_index == Long.MIN_VALUE) {
			lStat.setNull(4, 4);
		} else {
			lStat.setLong(4, iOrder_index);
		}
		int result = lStat.executeUpdate();

		// Retrieving the generated keys (if any).
		ResultSet lrsKeys = lStat.getGeneratedKeys();
		ResultSetMetaData lrsmKeys = lrsKeys.getMetaData();
		int colCount = lrsmKeys.getColumnCount();
		iKeys = new Object[colCount];
		while(lrsKeys.next()) {
			for(int i=0;i<iKeys.length;i++) {
				iKeys[i] = lrsKeys.getObject(i+1);
			}
		}
		lrsKeys.close();
		lStat.close();
		this.iUpdated = false;
		return result;
	}

	/**
	 * This method will return the automatically generated key for the insert if 
	 * one was triggered, or 'null' otherwise.
	 *
	 * @return	Object[]	with the generated keys.
	 */
	public Object[] getGeneratedKeys() {
		return this.iKeys;
	}

}